package org.zy.core;
 
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
 
public class MessageDecoder extends LengthFieldBasedFrameDecoder {
 
	public MessageDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment,
						  int initialBytesToStrip) {
		super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
	}
 
	public MessageDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment,
						  int initialBytesToStrip, boolean failFast) {
		super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
	}
 
	@Override
	protected Message decode(ChannelHandlerContext ctx, ByteBuf in2) throws Exception {
		ByteBuf in = (ByteBuf) super.decode(ctx, in2);
		if (in == null) {
			return null;
		}
 
		if (in.readableBytes() < 4) {
			return null;
		}
 
		Message myMsg = new Message();
		int dataLength = in.readInt();
		byte type = in.readByte();
		myMsg.setType(type);
		byte[] data = new byte[dataLength - 5];
		in.readBytes(data);
		myMsg.setData(data);
		in.release();
 
		return myMsg;
	}
}